package com.srd.phone.utils.verifycode;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.shengrongdai.srd.R;
import com.srd.phone.utils.TextSizeUtils;

public class CheckView extends View implements CheckAction {
    private boolean isRefresh = false;
    Context mContext;
    char[] CheckNum = {0, 0, 0, 0,0};
    Paint mTempPaint = new Paint();
    int textSize = 0;

    // 验证码

    public CheckView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mTempPaint.setAntiAlias(true);
        textSize = getResources().getDimensionPixelOffset(R.dimen.text_size_16);
        mTempPaint.setTextSize(textSize);
        mTempPaint.setStrokeWidth(3);
    }

    private int[] checkArr = new int[5]; // 验证码
    private int[][] lineArr = new int[ConmentConfig.LINE_NUM][5]; // 线
    private int[][] pointArr = new int[ConmentConfig.POINT_NUM][2]; //点

    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.GREEN);
        // 绘制验证码
        final int height = getHeight();
        final int width = getWidth();
        int dx = TextSizeUtils.Dp2Px(mContext,10);
        for (int i = 0; i < CheckNum.length; i++) {
            if (isRefresh == false) {
                canvas.drawText("" + CheckNum[i], dx, checkArr[i], mTempPaint);
            } else {
                int checkHeight = getPositon(height, textSize);
                checkArr[i] = checkHeight;
                canvas.drawText("" + CheckNum[i], dx, checkHeight, mTempPaint);
            }
            dx += width / 5;
        }
        // 绘制线
        int[] line = null;
        for (int i = 0; i < ConmentConfig.LINE_NUM; i++) {
            if (isRefresh == false) {
                canvas.drawLine(lineArr[i][0], lineArr[i][1], lineArr[i][2], lineArr[i][3], mTempPaint);
            } else {
                line = getLine(height, width);
                lineArr[i] = line;
                canvas.drawLine(line[0], line[1], line[2], line[3], mTempPaint);
            }
        }
        // 绘制小圆点
        int[] point;
        for (int i = 0; i < ConmentConfig.POINT_NUM; i++) {
            if (isRefresh == false) {
                canvas.drawCircle(pointArr[i][0], pointArr[i][1], 1, mTempPaint);
            } else {
                point = getPoint(height, width);
                pointArr[i] = point;
                canvas.drawCircle(point[0], point[1], 1, mTempPaint);
            }
        }
        isRefresh = false;
    }

    public void setCheckNum(char[] chenckNum) {
        CheckNum = chenckNum;
        //初始化数组
        checkArr = new int[CheckNum.length];
        lineArr = new int[ConmentConfig.LINE_NUM][CheckNum.length];
    }

    public char[] getCheckNum() {
        return CheckNum;
    }

    public void invaliChenkNum() {
        isRefresh = true;
        invalidate();
    }

    private int[] getLine(int height, int width) {
        int[] tempCheckNum = {0, 0, 0, 0};
        for (int i = 0; i < tempCheckNum.length; i += 2) {
            tempCheckNum[i] = (int) (Math.random() * width);
            tempCheckNum[i + 1] = (int) (Math.random() * height);
        }
        return tempCheckNum;
    }

    private int[] getPoint(int height, int width) {
        int[] tempCheckNum = {0, 0, 0, 0};
        tempCheckNum[0] = (int) (Math.random() * width);
        tempCheckNum[1] = (int) (Math.random() * height);
        return tempCheckNum;
    }


    // 获取验证码的绘制位置
    private int getPositon(int height, int textSize) {
        int tempPositoin = (int) (Math.random() * height);
        if (tempPositoin < textSize) {
            tempPositoin = textSize;
        } else if (tempPositoin > (height - textSize)) {
            tempPositoin = height - textSize;
        }
        return tempPositoin;
    }
}
